<?php

namespace app\api\service;

use app\common\model\DrawModel;
use app\common\traits\CacheTrait;
use Php127\Douyin\Factory;

class ToolsService extends CommonService
{
    use CacheTrait;
    public function weather(array $params){

        if(strpos($params['msg'],'天气') === 0){
            $weather = strReplaceMessage('天气',$params['msg']);
            if(!$weather){
               $this->send_group_at_msg($params['robot_wxid'],$params['from_wxid'],$params['final_from_wxid'],$params['final_from_name'],"\r请发送 天气 xxx \r例如：天气 北京");exit();
            }
            $ret = json_decode(file_get_contents('https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid=&city='.$weather),true);
            if($ret &&  isset($ret['city'])){//没问题 返回
                $data = $ret['data'][0];
                $message = "  \r".$weather."天气\r".
                    "日期 : ".$data['day']."\r".
                    '实时温度:'.$data['tem']."\r".
                    '最高温度:'.$data['tem1']."\r".
                    '最低温度:'.$data['tem2']."\r".
                    "风向 : ".implode('-',$data['win'])."\r".
                    "风力 : ".$data['win_speed']."\r".
                    "实时天气 : ".$data['wea']."\r".
                    "空气质量 : ".$data['air_level']."\r".
                    "紫外线 : ".$data['uvDescription']."\r".
                    "--------------------------\r" .
                    "输入`菜单`指令 了解更多技能\r";
                $this->send_group_at_msg($params['robot_wxid'],$params['from_wxid'],$params['final_from_wxid'],$params['final_from_name'],$message);
                exit();
            }
        }
    }

    public function sendMusic(array $params){
        if(strpos($params['msg'],'点歌') === 0){
            $music = strReplaceMessage('点歌',$params['msg']);
            if(!$music){
                $this->send_group_at_msg($params['robot_wxid'],$params['from_wxid'],$params['final_from_wxid'],$params['final_from_name'],"发送 点歌 歌名 例如：点歌 李白");exit();
            }
            $this->send_music_msg($params['robot_wxid'],$params['from_wxid'],trim($music));
            exit();
        }
    }

    public function checkPhone($params){
        if(strpos($params['msg'],'电话号码') === 0){
            $phone = strReplaceMessage('电话号码',$params['msg']);
            if(!$phone){
                $this->send_group_at_msg($params['robot_wxid'],$params['from_wxid'],$params['final_from_wxid'],$params['final_from_name'],"\r请发送 电话号码 xxxxx \r例如：电话号码 15888888888");exit();
            }
            $ret = json_decode(file_get_contents('https://api.vvhan.com/api/saorao?tel='.$phone),true);
            if($ret && $ret['success']){//没问题 返回
                $msg = "号码:".$phone.PHP_EOL.
                    "归属地:".$ret['info']['province'].$ret['info']['city'].PHP_EOL.
                    "运营商:".$ret['info']['operator'].PHP_EOL;
                    foreach ($ret['data'] as $v){
                        $msg.= $v['name'].':'.$v['msg'].PHP_EOL;
                    }
                $this->send_group_at_msg($params['robot_wxid'],$params['from_wxid'],$params['final_from_wxid'],$params['final_from_name'],$msg);
                exit();
            }
        }
    }

    public function constellation(array $params){
        if(strpos($params['msg'],'星座') === 0){
            $constellation = strReplaceMessage('星座',$params['msg']);
            if(!$constellation){
                $this->send_group_at_msg($params['robot_wxid'],$params['from_wxid'],$params['final_from_wxid'],$params['final_from_name'],"\r请发送 星座 xxx \r例如：星座 白羊");exit();
            }
            $name = config('utils.constellation')[$constellation];
            $url = sprintf('https://api.vvhan.com/api/horoscope?type=%s&time=today',$name);
            $ret = json_decode(file_get_contents($url),true);
            if($ret && $ret['success']){//没问题 返回
               $data = $ret['data'];
                $message = "  \r".$data['title']."\r".
                    "幸运颜色 : " . $data['luckycolor'] . "\r" .
                    "幸运数字 : " . $data['luckynumber'] . "\r" .
                    "速配星座 : " . $data['luckyconstellation'] . "\r" .
                    "运势解析 :\r" .
                    "    综合运势：" . $data['fortunetext']['all'] . "\r" .
                    "    爱情运势：" . $data['fortunetext']['love'] . "\r" .
                    "    学业工作：" . $data['fortunetext']['work'] . "\r" .
                    "    财富运势：" . $data['fortunetext']['money'] . "\r" .
                    "    健康运势：" . $data['fortunetext']['health'] . "\r" .
                    "总结 : " . $data['shortcomment'];
                $this->send_group_at_msg($params['robot_wxid'],$params['from_wxid'],$params['final_from_wxid'],$params['final_from_name'],$message);
                exit();
            }
        }
    }

    public function dogDiary(array $params){
        $ret = json_decode(file_get_contents('http://api.qemao.com/api/tiangou/'),true);
        if($ret['code'] == 200){
            $message = $ret['text'];
            $this->send_text_msg($params['robot_wxid'],$params['from_wxid'],$message);
        }
        exit();
    }

    public function dyAnalysis(array $params){
        $test = "/http[s]?:\/\/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+/";
        preg_match($test, $params['msg'], $match);
        if($match){
          (new ShortUrlService())->getDyVideo($params,$match[0]);
        }
        return false;
    }

    public function getDraw(array $params){
        //判断用户今天是否抽过签
        $key = $params['final_from_wxid'].'_'.$params['from_wxid'];
        $ret =$this->getCache($key);
        if($ret){
           $this->send_group_at_message($params,'今天已经抽过签了');exit();
        }
        //开始抽签
        $rands = rand(1,106);
        $draw = (new DrawModel())->where('id',$rands)->find();
        $msg = "第".$draw['id']."签".PHP_EOL.
            "----------------------".PHP_EOL.
            "签诗：".strReplaceMessage(' ',strReplaceMessage('\r\n\r\n',$draw['poetry'])).PHP_EOL.//清理不必要的参数
            "----------------------".PHP_EOL.
            "需要解签者发送 `解签`";
        $this->send_group_at_message($params,$msg);
        //将解签数据放入redis
        //计算今天的剩余时间
        $lowTime = strtotime(date('Y-m-d 23:59:59',time())) - time();
        $this->setCache($key,$draw['open_poetry'],$lowTime);
        //结束进程
        exit();
    }

    public function getOpenDraw(array $params){
        $key = $params['final_from_wxid'].'_'.$params['from_wxid'];
        $ret =$this->getCache($key);
        if(!$ret){
            $this->send_group_at_message($params,'请先抽签后解签');exit();
        }
        $msg = PHP_EOL."----------------------".PHP_EOL."解签：".$ret;
        $this->send_group_at_message($params,$msg);exit();
    }

}